﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ControlLibary
{
    /// <summary>
    /// NativePanel
    /// 支持设置边框
    /// </summary>
    public class NativePanel : Panel
    {

        private Color borderColor = Color.FromArgb(46, 46, 46);

        [Browsable(true), Description("边框颜色"), Category("自定义分组")]
        public Color BorderColor
        {
            get { return borderColor; }
            set
            {
                borderColor = value;
                this.Invalidate();
            }
        }

        private int borderSize = 1;

        [Browsable(true), Description("边框粗细"), Category("自定义分组")]
        public int BorderSize
        {
            get { return borderSize; }
            set
            {
                borderSize = value;
                this.Invalidate();
            }
        }

        /// <summary>
        /// 默认绘制四个边框
        /// </summary>
        private DrawDirect direct = DrawDirect.Bottom | DrawDirect.Left | DrawDirect.Right | DrawDirect.Top;

        public DrawDirect Direct
        {
            get { return direct; }
            set { direct = value; }
        }


        protected override void OnSizeChanged(EventArgs e)
        {
            this.Refresh();
            base.OnSizeChanged(e);
        }


        /// <summary>
        /// 重写OnPaint方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            //  DrawDirect.Top: 1
            //  DrawDirect.Bottom: 2
            //  DrawDirect.Left: 3
            //  DrawDirect.Right: 4

            //以上能构成的不完全排列组合:
            //1
            //12
            //13
            //14
            //123
            //124
            //134
            //1234

            //2
            //23
            //24
            //234

            //3
            //34

            //4
            //共15种情形


            Rectangle rec = ClientRectangle;
            //left top right bottom
            switch (direct)
            {
                case DrawDirect.Top:  //情形1 

                    ControlPaint.DrawBorder(e.Graphics,
                         rec,
                         this.borderColor,
                         0,
                         ButtonBorderStyle.Solid,
                         this.borderColor,
                         this.borderSize,
                         ButtonBorderStyle.Solid,
                         this.borderColor,
                         0,
                         ButtonBorderStyle.Solid,
                         this.borderColor,
                         0,
                         ButtonBorderStyle.Solid);

                    break;
                case DrawDirect.Top | DrawDirect.Bottom://情形12
                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid);
                    break;
                case DrawDirect.Top | DrawDirect.Left://情形13
                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid);
                    break;
                case DrawDirect.Top | DrawDirect.Right://情形14
                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid);
                    break;
                case DrawDirect.Top | DrawDirect.Bottom | DrawDirect.Left://情形123 
                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid);
                    break;
                case DrawDirect.Top | DrawDirect.Bottom | DrawDirect.Right://情形124
                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid);

                    break;
                case DrawDirect.Top | DrawDirect.Left | DrawDirect.Right://情形134
                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid);
                    break;
                case DrawDirect.Top | DrawDirect.Bottom | DrawDirect.Left | DrawDirect.Right://情形1234
                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid);
                    break;

                case DrawDirect.Bottom://情形2
                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid);
                    break;
                case DrawDirect.Bottom | DrawDirect.Left://情形23
                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid);
                    break;
                case DrawDirect.Bottom | DrawDirect.Right://情形24

                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid);
                    break;
                case DrawDirect.Bottom | DrawDirect.Left | DrawDirect.Right://情形234

                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid);
                    break;

                case DrawDirect.Left://情形3

                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid);
                    break;
                case DrawDirect.Left | DrawDirect.Right://情形34
                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid);
                    break;

                case DrawDirect.Right://情形4
                    ControlPaint.DrawBorder(e.Graphics,
                        rec,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        0,
                        ButtonBorderStyle.Solid,
                        this.borderColor,
                        this.borderSize,
                        ButtonBorderStyle.Solid);
                    break;
                default:
                    ControlPaint.DrawBorder(e.Graphics,
                                    rec,
                                    this.borderColor,
                                    this.borderSize,
                                    ButtonBorderStyle.Solid,
                                    this.borderColor,
                                    this.borderSize,
                                    ButtonBorderStyle.Solid,
                                   this.borderColor,
                                    this.borderSize,
                                    ButtonBorderStyle.Solid,
                                    this.borderColor,
                                    this.borderSize,
                                    ButtonBorderStyle.Solid);
                    break;
            }
        }
    }

    [Flags]
    public enum DrawDirect
    {
        Top = 1,
        Bottom = 2,
        Left = 4,
        Right = 8
    }
}
